package com.java.day4collection;

import java.util.Iterator;

/**
 * 班级
 */
public class MyClass implements Iterator<String>,Iterable<String> {

    //数据
    private String[] stuNames = {"张三","李四","王五","赵六","周七","刘八","陈九"};
    //当前下标
    private int index = -1;

    @Override
    public boolean hasNext() {
        //如果下标没有到最后一位
        return index < stuNames.length - 1;
    }

    @Override
    public String next() {
        //向后移动下标
        index++;
        //返回当前数据
        return stuNames[index];
    }

    @Override
    public Iterator<String> iterator() {
        return this;
    }

    public static void main(String[] args) {
        MyClass myClass = new MyClass();
        for(String name : myClass){
            System.out.println(name);
        }
//        //循环判断是否还有数据
//        while(myClass.hasNext()){
//            //向后移动，并获得数据
//            String s = myClass.next();
//            System.out.println(s);
//        }
    }


}
